package com.wine.exam.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wine.common.core.domain.AjaxResult;
import com.wine.common.utils.DateUtils;
import com.wine.common.utils.StringUtils;
import com.wine.exam.dto.SubjectDto;
import com.wine.exam.mapper.SubjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wine.exam.domain.Subject;
import com.wine.exam.service.ISubjectService;

/**
 * 学科Service业务层处理
 *
 * @author wine
 * @date 2022-09-04
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements ISubjectService {
    @Autowired
    private SubjectMapper subjectMapper;


    /**
     * 分页查询并根据名称模糊查询
     *
     * @param subjectDto
     * @return page and queryName 分页列表
     */
    @Override
    public Page<Subject> pageQueryList(SubjectDto subjectDto) {

        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<>();
//        参数1位空时，不加入这个条件。
        queryWrapper.like(StringUtils.isNotEmpty(subjectDto.getSubjectName()), Subject::getSubjectName, subjectDto.subjectName);

        Page<Subject> page = new Page<>();
        page.setCurrent(subjectDto.getPageNum());
        page.setSize(subjectDto.getPageSize());

        Page<Subject> list = subjectMapper.selectPage(page, queryWrapper);

        return list;
    }


    /**
     * 根据学科id查询详细 此信息只有管理端查看，因此返回所有数据字段也行
     *
     * @param subjectId
     * @return subject详情
     */
    @Override
    public Subject getInfo(Long subjectId) {
        return subjectMapper.selectById(subjectId);
    }

    /**
     * 插入学科
     *
     * @param subject
     * @return 影响行数
     */
    @Override
    public int add(Subject subject) {
        return subjectMapper.insert(subject);
    }


    /**
     * 修改学科
     *
     * @param subject
     * @return 影响行数
     */
    @Override
    public int edit(Subject subject) {
        return subjectMapper.updateById(subject);
    }


    @Override
    public int delByIds(Long[] subjectIds) {
        return subjectMapper.deleteBatchIds(Arrays.asList(subjectIds));
    }

    /**
     * 修改状态。
     *
     * @param subjectId
     * @param status
     * @return
     */
    @Override
    public int updateStatus(Long subjectId, String status, String updateBy) {

        return subjectMapper.updateStatus(subjectId, status, updateBy);
    }

}
